package com.weedsdream.app.colorplate

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.weedsdream.app.R

/**强调选中色笔迹，true-使用蒙版弱化其它笔迹内容*/
var emphasizeCurColor by mutableStateOf(false)

/**最大笔迹尺寸*/
const val MAX_PEN_SIZE = 99f

/**笔迹尺寸*/
var penSize by mutableFloatStateOf(8f)


/**橡皮擦选中时*/
@Preview(widthDp = 600, heightDp = 350)
@Composable
fun PenValue() {
    val height = with(LocalDensity.current) { MAX_PEN_SIZE.toDp() }
    Row(
        Modifier
            .padding(5.dp)
            .height(maxOf(height, 30.dp))
    ) {
        Row(Modifier
            .align(Alignment.CenterVertically)
            .pointerInput(Unit) {
                detectTapGestures { emphasizeCurColor = !emphasizeCurColor }
            }) {
            Text(text = "强调选中色", modifier = Modifier.align(Alignment.CenterVertically))
            Icon(
                painterResource(
                    if (emphasizeCurColor) R.drawable.select_yes else R.drawable.select_no
                ), null, modifier = Modifier
                    .size(30.dp)
                    .align(Alignment.CenterVertically),
                Color.Unspecified
            )
        }
        Spacer(Modifier.width(20.dp))
        Text("笔迹粗细:", modifier = Modifier.align(Alignment.CenterVertically))
        Text(
            "${penSize.toInt()}", modifier = Modifier
                .align(Alignment.CenterVertically)
                .width(25.dp)
        )
        Canvas(Modifier
            .height(height)
            .weight(1f)
            .pointerInput(Unit) {//滑动时重设笔迹
                detectDragGestures { change, _ -> setSize(change.position, size.width) }
            }
            .pointerInput(Unit) {//触摸时重设笔迹
                detectTapGestures(onPress = { setSize(it, size.width) })
            }) {
            //画笔迹三角示范区域
            val path1 = Path().apply {
                moveTo(0f, size.height / 2)
                lineTo(size.width, 0f) // 左侧底边顶点
                lineTo(size.width, size.height) // 右侧底边顶点
                close() // 关闭路径
            }
            drawPath(path1, color = Color.DarkGray) // 填充颜色
            //画选中线
            val x = size.width * penSize / MAX_PEN_SIZE
            drawLine(curRGB, Offset(x, 0f), Offset(x, size.height), 5f)
        }
        Spacer(modifier = Modifier.width(10.dp))
        Box(
            Modifier
                .size(height)
                .border(1.dp, curRGB)
        ) {
            Spacer(
                Modifier
                    .align(Alignment.Center)
                    .size(with(LocalDensity.current) { penSize.toDp() })
                    .background(curRGB)
            )
        }
    }
}

/**根据触摸位置修改尺寸
 * @param pos 触摸位置
 * @param width 触摸区域宽度*/
private fun setSize(pos: Offset, width: Int) {
    penSize = (MAX_PEN_SIZE * pos.x / width).let { size ->
        if (size < 1f) 1f else if (size > MAX_PEN_SIZE) MAX_PEN_SIZE else size
    }
}